home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-04-03 | 7.9 KB | 302 lines | [TEXT/MPS ] |
- //----------------------------------------------------------------------------------------
- // UPointerObject.cp
- // Copyright © 1984-96 by Apple Computer, Inc. All rights reserved.
- //----------------------------------------------------------------------------------------
-
- #ifndef __UPOINTEROBJECT__
- #include "UPointerObject.h"
- #endif
-
- // MacApp
-
- #ifndef __UFAILURE__
- #include "UFailure.h"
- #endif
-
- #ifndef __UCOREUTILITIES__
- #include "UCoreUtilities.h"
- #endif
-
- #ifndef __UMEMORY__
- #include "UMemory.h"
- #endif
-
- #ifndef __UOBJECT__
- #include "UObject.h"
- #endif
-
- #ifndef __UNIVERSALSTARTUP__
- #include "UniversalStartup.h"
- #endif
-
-
- // OpenDoc
-
- #ifndef _MEMMGR_
- #include "MemMgr.h"
- #endif
-
-
- // ANSI
-
- #ifndef __STDIO__
- #include <stdio.h>
- #endif
-
- #ifndef __STDLIB__
- #include <stdlib.h>
- #endif
-
- //========================================================================================
- // Defines
- //========================================================================================
-
- // The default is to use MacApp's object heap for non-TObject structures.
- // To revert to using calloc/malloc for structures, compile with -d qMAGlobalNew=0.
-
- #ifndef qMAGlobalNew
- #define qMAGlobalNew 1
- #endif
-
- //========================================================================================
- // Global variable definitions
- //========================================================================================
-
- Boolean pAllocateObjectsFromPerm = TRUE; // Used to track whether to
- // allocate objects from
- // permanent memory or not.
-
- //========================================================================================
- // GLOBAL Procedures
- //========================================================================================
- #undef Inherited
-
- inline Boolean IsPointer(void *pointer)
- {
- return (pointer != NULL && (((long)pointer & 1) != 1));
- }
-
- static Boolean VerboseIsPointer(void *pointer);
-
- //----------------------------------------------------------------------------------------
- // MAOperatorNew:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- void* MAOperatorNew(size_t size)
- {
- // The following block ensures that, if we are invoked before InitUMemory has
- // had a chance to instantiate gObjectHeap (i.e. at static initialization time), we
- // nevertheless instantiate a minimal ObjectHeap. Note that it will be expanded
- // later on in InitUMemory to reflect the memory characteristics required by the app.
-
- if (gObjectHeap == NULL)
- {
- gObjectHeap = MMNewHeap( kMMAppMemory, (size_t)gSizeObjectHeap, (size_t)gSizeHeapIncrement,
- "Default MacApp Heap" );
-
- // Set the default heap if none has been set yet
- if (!MMGetDefaultHeap())
- MMSetDefaultHeap(gObjectHeap);
- }
-
- void* blk = MMAllocateIn( size, gObjectHeap );
- FailNIL(blk);
-
- return blk;
- }
-
- //----------------------------------------------------------------------------------------
- // MAOperatorDelete:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- void MAOperatorDelete(void* obj)
- {
- #if qDebug
- if (!gObjectHeap)
- ProgramBreak("operator delete invoked prior to gObjectHeap being created!");
- else
- #endif
- MMFree(obj);
- }
-
- #if qMAGlobalNew
-
- //----------------------------------------------------------------------------------------
- // operator new:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- void* operator new(size_t size)
- {
- return MAOperatorNew(size);
- }
-
- //----------------------------------------------------------------------------------------
- // operator delete:
- //----------------------------------------------------------------------------------------
- #pragma segment Main
-
- void operator delete(void* obj)
- {
- MAOperatorDelete(obj);
- }
-
- #endif // qMAGlobalNew
-
- //----------------------------------------------------------------------------------------
- // GetPermObjectAllocationState:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- Boolean GetPermObjectAllocationState()
- {
- return pAllocateObjectsFromPerm;
- } // GetPermObjectAllocationState
-
- //----------------------------------------------------------------------------------------
- // AllocateObjectsFromPerm:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- Boolean AllocateObjectsFromPerm(Boolean allocateFromPerm)
- {
- Boolean previousState = pAllocateObjectsFromPerm;
- pAllocateObjectsFromPerm = allocateFromPerm;
- return previousState;
- } // AllocateObjectsFromPerm
-
- //----------------------------------------------------------------------------------------
- // FailNonObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- void FailNonObject(TObject* obj)
- {
- if (!IsObject(obj))
- {
- #if qDebugMsg
- char msg[80];
-
- VerboseIsObject(obj); // show why
- sprintf(msg, "Object that failed discipline %p", obj);
- ProgramBreak(msg);
- #endif
- Failure(minErr, 0);
- }
- } // FailNonObject
-
- //----------------------------------------------------------------------------------------
- // FreeIfObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- TObject* FreeIfObject(TObject* obj)
- {
- if (obj)
- {
- #if qDebug
- if (!VerboseIsObject(obj))
- ProgramBreak("In FreeIfObject: Not handed a valid object.");
- else
- // only free obj if it passes VerboseIsObject…
- #endif
-
- obj->Free();
- }
- return NULL;
- } // FreeIfObject
-
- //----------------------------------------------------------------------------------------
- // InitUObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MAInit
-
- void InitUObject()
- {
- ClassDesc::InitUClassDesc();
- } // InitUObject
-
- //----------------------------------------------------------------------------------------
- // VerboseIsPointer:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- Boolean VerboseIsPointer(void *pointer)
- {
- // we should try to find a way to ask the memory manager if this is a valid pointer
- return IsPointer(pointer);
- } // VerboseIsPointer
-
-
- //----------------------------------------------------------------------------------------
- // IsObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MAObjectRes
-
- Boolean IsObject(TObject *obj)
- {
- // Test objecthood
-
- if ( !IsPointer(obj) )
- return FALSE;
-
- #if qDebug
- if (gObjectHeap != NULL)
- {
- // check for valid heap block
- //SRF -temporary if (!gObjectHeap->IsValidBlock(obj))
- // if (!gObjectHeap->IsValidBlock(obj))
- // return FALSE;
-
- // check for valid class ID
- if (!ClassDesc::IsValidClassID(obj->fClassID))
- return FALSE;
- }
- #endif
-
- return TRUE;
- } // IsObject
-
-
- //----------------------------------------------------------------------------------------
- // VerboseIsObject:
- //----------------------------------------------------------------------------------------
- #pragma segment MADebug
-
- Boolean VerboseIsObject(TObject *obj)
- {
- if (VerboseIsPointer(obj))
- {
- #if qDebug
- if (!ClassDesc::IsValidClassID(obj->fClassID))
- fprintf(stderr, " That pointer is not a subclass of TObject.\n");
- else
- {
- if (gObjectHeap != NULL)
- {
- // check for valid heap block
- //SRF -temporary if (!gObjectHeap->IsValidBlock(obj))
- // {
- // fprintf(stderr, " That pointer is not a valid heap block.\n");
- // }
- // else
- return TRUE;
- }
- else
- return TRUE;
- }
- #else
- return TRUE;
- #endif
- }
-
- return FALSE;
- } // VerboseIsObject
-
- //----------------------------------------------------------------------------------------
- // End of UPointerObject.cp
-
- #pragma segment Inline
-